package com.example.blog.controller;
import com.example.blog.domain.Response;
import com.example.blog.note.NoToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import java.security.MessageDigest;
        import java.security.NoSuchAlgorithmException;
        import java.util.Arrays;

import static com.example.blog.utils.IdUtils.sha1;

/**
 * @description:
 * @author: lvyq
 * @date: 2022/7/8 15:16
 * @version 1.0
 */

@RequestMapping("/weixin")
@Controller
@CrossOrigin
@Slf4j
public class WeiXInController {

    public static final String Token = "lyzhnb"; //微信公众平台中设置的token


    @NoToken
    @GetMapping
    public String index(HttpServletRequest request) throws Exception {
        String method = request.getMethod();
        //签名验证是get请求
            // 微信加密签名
            String signature = request.getParameter("signature");
            // 随机字符串
            String echostr = request.getParameter("dcNJfhmqVX8HaT0gY7Y37TDMabCzptsPVUR9Zd5BdcY");
            // 时间戳
            String timestamp = request.getParameter("timestamp");
            // 随机数
            String nonce = request.getParameter("nonce");
            String[] str = {Token, timestamp, nonce};
            // 字典排序
            Arrays.sort(str);
            String bigStr = str[0] + str[1] + str[2];
            // SHA1加密
            String digest = sha1(bigStr);
            // 确认请求来至微信
            if (digest.equals(signature)) {
                return digest;
            } else {
                log.info("失败！！！！！！！！！");
                return "失败";
            }
    }
}
